﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;

namespace DAL.Repositories
{
    public class MovieRepository : RepositoryInterfaces.IMovieRepository
    {
        /// <summary>
        /// Get all movies
        /// </summary>
        /// <returns>All movies</returns>
        /// <exception cref="ArgumentNullException">If there exist no movies</exception>
        /// <exception cref="Exception">Other exceptions</exception>
        public IList<Movie> GetMovies()
        {
            try
            {
                using (BalderEntities _db = new BalderEntities())
                {
                    return _db.Movies.ToList();
                }
            }
            catch (ArgumentNullException e)
            {
                throw new ArgumentNullException("No movies found" + Environment.NewLine + e.Message);
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// Get all movies for cinema, with cinema object added
        /// </summary>
        /// <returns>All movies in cinema with cinema object attached</returns>
        /// <exception cref="ArgumentNullException">If no movies exist</exception>
        /// <exception cref="Exception"></exception>
        public IList<Movie> GetMovies(Cinema cinema)
        {
            try
            {
                if (cinema != null)
                {
                    using (BalderEntities _db = new BalderEntities())
                    {
                        return (from m in _db.Movies
                                from c in m.Cinemas
                                where c.Id == cinema.Id
                                select m).ToList();
                    }
                }
                else
                {
                    throw new ArgumentNullException("Cinema is null!");
                }
            }
            catch (ArgumentNullException e)
            {
                throw new ArgumentNullException("No movies found" + Environment.NewLine + e.Message);
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// Add specific movie
        /// </summary>
        /// <returns>none</returns>
        /// <exception cref="ArgumentNullException">If movie does already exist</exception>
        /// <exception cref="Exception"></exception>
        public void AddMovie(Movie movie)
        {
            try
            {
                if (movie != null)
                {
                    using (BalderEntities _db = new BalderEntities())
                    {
                        _db.Cinemas.Attach(movie.Cinemas.First());
                        _db.Movies.Add(movie);
                        _db.SaveChanges();
                    }
                }
                else
                {
                    throw new ArgumentException("Movie object is null");

                }
            }
            catch (Exception)
            {
                throw;
            }   
        }

        /// <summary>
        /// Get movie from id
        /// </summary>
        /// <param name="id">Id of movie</param>
        /// <returns>Movie with Id</returns>
        /// <exception cref="Exception"></exception>
        public Movie GetMovie(int id)
        {
            try
            {
                using (BalderEntities _db = new BalderEntities())
                {
                    return _db.Movies.Include(m => m.Actors).Where(m => m.Id == id).Single();
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}